package com.atguigu.gmall.payment.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author: Mamba
 * @version: 1.0
 * @Time: 2021/1/14 18:31
 * @Description:
 */
@Controller
@RequestMapping("/api/payment/alipay")
public class AlipayController {

    @Autowired
    private AlipayService alipayService;
    @Autowired
    private PaymentService paymentService;

    //去支付宝进行支付
    @GetMapping("/submit/{orderId}")
    @ResponseBody
    public String submitAliPay(@PathVariable Long orderId){

        return alipayService.submitAliPay(orderId);
    }
    //同步通知
    @GetMapping("/callback/return")
    public String callbackReturn(){
        return "redirect:" + AlipayConfig.return_order_url;
    }

    //支付宝公司异步通知
    @PostMapping("/callback/notify")
    @ResponseBody
    public String callbackNotify(@RequestParam Map<String, String> paramsMap){
        //boolean signVerified = false;
        System.out.println("+++++++++++++++++++");

        boolean signVerified = false;
        try {
            signVerified = AlipaySignature.rsaCheckV1(
                    paramsMap,
                    AlipayConfig.alipay_public_key,
                    AlipayConfig.charset,
                    AlipayConfig.sign_type
            );

        if (signVerified){
                //验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商户自身业务处理，校验失败返回failure
                if ("TRADE_SUCCESS".equals(paramsMap.get("trade_status"))){
                    //交易成功
                    paymentService.updatePaymentInfo(paramsMap);

                    System.out.println(paramsMap);
                    return "success";
                    //{gmt_create=2021-01-15 19:15:10,
                    // charset=utf-8,
                    // gmt_payment=2021-01-15 19:15:27,
                    // notify_time=2021-01-15 19:15:29,
                    // subject=Apple iPhone 11 (A2223) 256GB 黄色 移动联通电信4G手机 双卡双待 Redmi K30 5G双模 120Hz流速屏 骁龙765G 30W快充 6GB+128GB 深海微光,
                    // buyer_id=2088622955321863,
                    // invoice_amount=90980.00,
                    // version=1.0,
                    // notify_id=2021011500222191528021860510328030,
                    // fund_bill_list=[{"amount":"90980.00","fundChannel":"ALIPAYACCOUNT"}],
                    // notify_type=trade_status_sync,
                    // out_trade_no=ATGUIGU161070929550515,
                    // total_amount=90980.00,
                    // trade_status=TRADE_SUCCESS,
                    // trade_no=2021011522001421860501547424,
                    // auth_app_id=2021000116694889,
                    // receipt_amount=90980.00,
                    // point_amount=0.00,
                    // app_id=2021000116694889,
                    // buyer_pay_amount=90980.00,
                    // seller_id=2088621955145141}


                }else {
                    // 验签失败则记录异常日志，并在response中返回failure.
                    System.out.println("验签失败");
                    return "failure";
                }
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return "failure";
    }
}
